package parkingManage.utils;

import java.util.ResourceBundle;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtils {

	private static ResourceBundle rb = ResourceBundle.getBundle("parkingManage.jdbc");

	// 配置属性文件
	private static String classname = rb.getString("driver");
	private static String url = rb.getString("url");
	private static String user = rb.getString("user");
	private static String pass = rb.getString("pass");

	/**
	 * 加载驱动，获取连接
	 */

	public static Connection getConnection() {
		Connection conn = null;
		try {
			// 1-加载驱动
			Class.forName(classname);

			// 2-获取连接
			conn = DriverManager.getConnection(url, user, pass);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	/**
	 * 关闭数据库资源
	 */
	public static void closeAll(Statement sta, Connection conn, ResultSet rs) {
		// 关闭数据库资源
		try {
			if (rs != null) {
				rs.close();
			}

			if (sta != null) {
				sta.close();
			}

			if (conn != null) {
				conn.close();
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
